﻿package com.floorplanner.lib3ds
{
	import com.floorplanner.lib3ds.*;
	import flash.utils.ByteArray;
	
	public class Lib3dsIO extends ByteArray
	{
		public function Lib3dsIO()
		{
			
		}
	
		/**
		 * 
		 * @param	maxLength
		 * @return
		 */
		public function readString( maxLength:int = 64 ):String
		{
			var s:String = "";
			for( var i:int = 0; i < maxLength; i++ )
			{
				var b:int = readByte();
				if( !b ) break;
				s +=  String.fromCharCode( b );
			}
			return s;
		}
		
		/**
		 * 
		 * @return
		 */
		public function readDWord():int
		{
			var a:int = readUnsignedByte();
			var b:int = readUnsignedByte();
			var c:int = readUnsignedByte();
			var d:int = readUnsignedByte();
			return d << 24 | c << 16 | b << 8 | a;
		}
		
		/**
		 * 
		 */
		public function readPercentage():Number 
		{
			var c:Lib3dsChunk = Lib3dsChunk.read( this );
			var chunk:uint = c.next( this );
			var p:Number = 0;
			
			while( chunk != 0 )
			{
				switch( chunk )
				{
					case Lib3dsChunk.INT_PERCENTAGE:
						var s:Number = readShort();
						p = (1.0 * s / 100.0);
						break;
					default:
						break;
				}
				chunk = c.next( this );
			}
			this.position = c.end;
			return p;
		}
		
		/**
		 * 
		 * @return
		 */
		public function readVector():Array
		{
			var v:Array = new Array(3);
			v[0] = readFloat();
			v[1] = readFloat();
			v[2] = readFloat();
			return v;
		}
		
		/**
		 * 
		 * @return
		 */
		public function readWord():uint
		{
			return readUnsignedShort();
		}
	}
}
